package com.link.base.base.product.controller;


import com.link.base.user.model.CoreUser;
import com.link.base.base.product.model.Tag;
import com.link.base.base.product.service.TagService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 标签
 */
@Controller
@RequestMapping("/link/tag")
public class TagController extends BasicController<Tag> {

    @Resource
    private TagService tagService;

    @Override
    public BasicService<Tag> getBasicService() throws Exception {
        return tagService;
    }

    /**
     * 删除一条记录
     */
    @Override
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@JsonParam Tag entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-031");
            }
            CoreUser user = UserUtil.getUser();
            entity.setLoginCorpId(user.getLoginCorpId());
            tagService.deleteTagAndProdById(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询产品标签列表基础数据
     */
    @RequestMapping(value = "/queryProdTags")
    @ResponseBody
    public Map<String, Object> queryProdTags(Tag guideProdTag, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Tag> list = tagService.queryProdTags(guideProdTag);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
